import { createI18n } from 'vue-i18n'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
import zhLocale from './zh-cn'
import enLocale from './en'

// 合并Element Plus和自定义语言包
const messages = {
  'zh-CN': {
    ...zhLocale.message,
    el: zhCn.el
  },
  'en': {
    ...enLocale.message,
    el: en.el
  }
}

// 从localStorage获取语言设置，如果没有则默认使用中文
const getCurrentLocale = () => {
  const storedLocale = localStorage.getItem('locale')
  return storedLocale || 'zh-CN'
}

const i18n = createI18n({
  legacy: false, // 使用Composition API模式
  locale: getCurrentLocale(),
  fallbackLocale: 'zh-CN',
  messages
})

export default i18n

// 切换语言的方法
export const switchLanguage = (locale: 'zh-CN' | 'en') => {
  i18n.global.locale.value = locale
  localStorage.setItem('locale', locale)
}